//
//  LikeList.swift
//  StorePlan
//
//  Created by admin on 2020/9/22.
//  Copyright © 2020 admin. All rights reserved.
//

import SwiftUI
import WaterfallGrid
import Alamofire
import YYKit
import SwiftyJSON

class Category :NSObject{
    
    @objc var id:NSString?
    @objc var name:NSString?
}
struct LikeList: View {
    
    @State var categorys:Array<Any>
    @State var showadd = false
    func getlikes() {
        
        AF.request("\(baseURL)sharelike/listcategory",method: .get)
            .responseJSON { response in
                switch response.result {
                case .success(let value):
                    let json = JSON(value)
                    let data = json["data"]
                    self.categorys = NSArray.modelArray(with: Category.self, json: data.rawValue) ?? []
                    print(self.categorys)
                case .failure(let error):
                    print("\(error)")
                }
            }
    }
    
    var body: some View {
        
        NavigationView {
            ZStack {
                
                List(0..<self.categorys.count, id: \.self) { index in
                    HStack {
                        Text("\(index)")
                        Text("\((self.categorys[index] as! Category).name ?? "default value")")
                    }
                }
                
                
                VStack{
                    Spacer()
                    
                    Button(action: {
                        self.showadd.toggle()
                    }) {
                        Image(systemName: "plus")
                            .imageScale(.large)
                            .foregroundColor(Color.white)
                            .font(.system(size: 16, weight: .medium))
                            .frame(width: 36, height: 36)
                            .background(Color("accent").opacity(0.8))
                            .clipShape(Circle())
                            .shadow(color: Color.black.opacity(0.1), radius: 1, x: 0, y: 1)
                            .shadow(color: Color.black.opacity(0.2), radius: 10, x: 0, y: 10)
                    }
                    
                    .sheet(isPresented: $showadd,onDismiss: {
                        self.getlikes()
                    }) {
                        CreateLikeCategory()
                    }
                    
                }
                
                .onAppear(perform: { self.getlikes() })
            }
            .navigationBarTitle("收藏", displayMode: .inline)
            .navigationBarItems(leading:
                        Button(action: {
                            print("SF Symbol button pressed...")
                            
                        }) {
                            Image(systemName: "text.justify")
                                .frame(width: 36, height: 36)
                                .foregroundColor(.black)
                                .background(Color.clear)
                                .clipShape(Circle())
                             
                        },
                    trailing:
                        Button(action: {
                            print("Edit button pressed...")
                        }) {
                            Text("Edit")
                        }
                )
        }
        
    }
}

struct LikeList_Previews: PreviewProvider {
    static var previews: some View {
        LikeList(categorys: _categorys)
    }
}

let json = JSON(arrayLiteral:
                    ["name":"Top50", "id": "0"],
                ["name":"农业龙头", "id": "0"],
                ["name":"ETF", "id": "2"]
            
)
let _categorys =  NSArray.modelArray(with: Category.self, json:json.rawValue) ?? []





